<template>
  <view class="address">
    <!-- 地址 -->
    <!-- {{systemWindowHeight}},{{systemPixelRatio}},{{systemScreenWidth}},{{systemScreenHeight}},{{systemWindowTop}},{{systemWindowBottom}} -->
    <scroll-view class="scroll-wrap" :scroll-y="true">
      <view class="address-list">
        <block v-for="(item, index) in addressList" :key="item.id">
          <view class="address-subItem">
            <view class="line-user-info">
              <view class="left">
                {{item.contact}}
              </view>
              <view class="right">
                {{item.contactMobile}}
              </view>
            </view>
            <view class="line-address">
              {{item.zone + item.street}}
            </view>
            <view class="line-operation">
              <view class="left">
                <view class="radio-wrap">
                  <radio :checked="item.byDefault == 1"  style="transform: scale(0.7)" @click="SetDefaultClick(item)"/>
                  <text class="default-text">默认地址</text>
                </view>
                
              </view>
              <view class="right">
                <view class="edit-btn choose-btn" v-if="sourceType" @click="chooseClick(item)">选择</view>
                <view class="edit-btn" @click="editClick(item)">编辑</view>
                <view class="del-btn" @click="deleteClick(item, index)">删除</view>
              </view>
            </view>
          </view>
          <view class="cut-line"></view>
        </block>
        
      </view>
    </scroll-view>
    
    <view class="uni-padding-wrap uni-common-mt">
      <button type="primary" @click="add_newaddress_click">添加新的地址</button>
    </view>
    <common-pop :baseInfo="commonPopInfo" @cancelPop="cancelComPop" @doSure="delectAddress"></common-pop>
  </view>

</template>

<script>
import {AddressDelete,AddressFindAll,AddressSetDefault} from '../../service/user/user.service.js'
import commonPop from '@/components/common-pop/index.vue'
export default{
  components: {commonPop},
  data () {
    return {
      byDefault: '1',
      addressList: [], // 地址列表
      pageInfo: {
        current: 1,
        size: 100,
        total: 0,
        length: 0
      },
      sourceType: null,
      sourceIsCar: null,
      commonPopInfo: {
        title: '确认该地址吗？',
        cancel: '取消',
        sure: '确定',
        show: false
      },
      delectId: '' // 删除地址的id
    }
  },
  onLoad:function(option) {
    this.sourceType = option.type;
    this.sourceIsCar = option.isCartBy;
  },
  created () {
    // this.getAddressList();
  },
  onShow () {
    // this.addressList = [];
    this.getAddressList();
  },
  methods:{
    cancelComPop () {
      this.commonPopInfo.show = false;
    },
    delectAddress () {
      AddressDelete({
        id: this.delectId,
      }, (res) => {
        if (res.status == 0) {
          // this.addressList.splice(index, 1);
          uni.showToast({
            title:' 删除成功 ！'
          })
          this.getAddressList();
          this.commonPopInfo.show = false;
        } else {
          uni.showToast({
            title:res.msg,
          })
        }
      })
    },
    getAddressList (size) { // 获取地址列表
      AddressFindAll({
        size: this.pageInfo.size,
        current: this.pageInfo.current
      }, (res) => {
        if (res.status == 0) {
          this.addressList = res.data.records;
        }
      })
    },
    SetDefaultClick (item) { // 设置默认地址
      if (item.byDefault == 1) {
        return false;
      }
      AddressSetDefault({
        id: item.id
      }, (res) => {
        if (res.status == 0) {
          let flag = true;
          item.byDefault = 1;
          let addressList = JSON.parse(JSON.stringify(this.addressList));
          addressList.forEach((addressitem, index) => {
            if (addressitem.id !== item.id && addressitem.byDefault == 1 && flag) {
              addressitem.byDefault = 0;
              this.addressList.splice(index, 1, addressitem);
            }
          });
          uni.showToast({
            title:'设置默认成功！'
          })
        } else {
          uni.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      })
    },
    //删除地址
    deleteClick (item, index) {
      this.delectId = item.id;
      this.commonPopInfo.show = true;
    },
    //编辑地址
    editClick (item) {
      if (this.sourceType) {
        if (this.sourceIsCar) {
          uni.navigateTo({
            url: '/pages/user/addAddress?id=' + item.id + '&type=' + 1 + '&isCartBy=' + 1
          })
        } else {
          uni.navigateTo({
            url: '/pages/user/addAddress?id=' + item.id + '&type=' + 1
          })
        }
      } else {
        uni.navigateTo({
          url: '/pages/user/addAddress?id=' + item.id
        })
      }
    },
    // 新增地址
    add_newaddress_click() {
      if (this.sourceType) {
        if (this.sourceIsCar) {
          uni.navigateTo({
            url: '/pages/user/addAddress?type=' + 1 + '&isCartBy=' + 1
          })
        } else {
          uni.navigateTo({
            url: '/pages/user/addAddress?type=' + 1
          })
        }
      } else {
        uni.navigateTo({
          url: '/pages/user/addAddress'
        })
      }
    },
    chooseClick (item) {
      uni.setStorage({
        key: 'orderAddress',
        data: item,
        success: () => {
          if (this.sourceIsCar) {
            uni.navigateTo({
              url: '/pages/purchaseShop/goodsSureOrder?isCartBy=' + 1
            })
          } else {
            uni.navigateTo({
              url: '/pages/purchaseShop/goodsSureOrder'
            })
          }
          console.log(item, 'item is')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .address {
    .scroll-wrap {
      height: calc(100vh - 52px);
      background-color: #FAFAFA;
    }
    // padding: 0 32rpx;
    .address-subItem {
      background-color: #FFFFFF;
    }
    
    .line-user-info {
      padding: 42rpx 32rpx 0;
      display: flex;
      justify-content: space-between;
      font-family: PingFang-SC-Medium;
      font-size: 28rpx;
      color: #333333;
      .left {}
      .right {}
    }
    .line-address {
      padding: 0 32rpx;
      margin-top: 36rpx;
      font-size: 28rpx;
      margin-bottom: 20rpx;
      word-break: break-word;
      word-wrap: break-word;
    }
    
    .line-operation {
      padding: 0 32rpx;
      height: 86rpx;
      border-top: 2rpx solid #E1E1E1;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        .radio-wrap {
          display: flex;
          align-items: center;
          .default-text {
            font-size: 24rpx;
            font-family: PingFang-SC-Medium;
            color: #333333;
            margin-left: 22rpx;
          }
        }
      }
      .right {
        margin-right: 32rpx;
        font-family: PingFang-SC-Medium;
        font-size: 24rpx;
        display: flex;
        align-items: center;
        .edit-btn {
          color: #1F8EDD;
        }
        .del-btn {
          margin-left: 90rpx;
          color: #FF7473;
        }
        .choose-btn {
          margin-right: 90rpx;
        }
      }
    }
    .cut-line {
      height: 22rpx;
      background-color: #FAFAFA;
    }
  }

  /* 下面的button按钮 */
  .uni-common-mt button {
    width: 687rpx;
    height: 70rpx;
    background: #434343;
    font-size:30rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: rgba(246, 218, 153, 1);
    margin:21rpx auto;
  }
  .uni-common-mt{
    width: 100%;
    position:fixed;
    bottom:0;
    height: 112rpx;
    background-color: #FFFFFF;
    border-top:2rpx solid #E1E1E1;
  }
 
</style>
<style>

</style>